package com.blockmeta.bbs.immodule.modelimpl;

import com.blankj.utilcode.util.StringUtils;
import com.blockmeta.bbs.businesslibrary.baselist.BaseBean;
import com.blockmeta.bbs.businesslibrary.baselist.BaseListModelImpl;
import com.blockmeta.bbs.businesslibrary.net.retrofit.RetrofitFactory;
import com.blockmeta.bbs.immodule.bean.LibraryBookBean;
import com.blockmeta.bbs.immodule.net.api.GetLibraryBookListApi;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

import java.util.ArrayList;
import java.util.HashMap;

import io.reactivex.Flowable;

public class MyLibraryBookModellImpl extends BaseListModelImpl<LibraryBookBean> {

    private String mKeyWord;

    public void search(RxAppCompatActivity context, String keyWord) {
        PAGE = 1;
        isMore = false;
        mKeyWord = keyWord;
        PostParams(context);
    }

    @Override
    public Flowable<LibraryBookBean> ServiceParams(HashMap<String, String> params) {
        return RetrofitFactory.getRootRetrofit().create(GetLibraryBookListApi.class).getLibraryBookList(params);
    }

    @Override
    public void SuccessOperation(BaseBean o) {
        LibraryBookBean libraryBookBean = (LibraryBookBean) o;
        if(libraryBookBean != null){
            ArrayList<?> datas = libraryBookBean.data.list;
            if(datas != null){
                if(isMore){
                    loadListeners.getDataMore(datas);
                }else {
                    loadListeners.getDataRefresh(datas);
                }
            }else {
                loadListeners.hasNoMoreData();
            }
        }else {

        }
    }

    @Override
    public HashMap<String, String> getParams() {
        HashMap<String, String> params = super.getParams();
        params.put("num", "10");
        if(!StringUtils.isEmpty(mKeyWord)){
            params.put("keyword", mKeyWord);
        }
        return mParams;
    }


}
